package com.be.beadmin.system.controller.ProjectDesignController;

import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.entity.ProductDrawingInfo;
import com.be.beadmin.design.repository.ProductDrawingInfoRepository;
import com.be.beadmin.project.entity.BatchProduct;
import com.be.beadmin.project.entity.PlanStage;
import com.be.beadmin.project.repository.BatchProductRepository;
import com.be.beadmin.project.repository.PlanStageRepository;
import com.be.beadmin.project.service.BatchProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


@Api(tags = {"柜体设计审核"})
@RestController
public class DrawingReviewPdController {

    @Autowired
    private BatchProductService batchProductService;

    @Autowired
    private BatchProductRepository batchProductRepository;

    @Autowired
    private ProductDrawingInfoRepository productDrawingInfoRepository;


    @Autowired
    private PlanStageRepository planStageRepository;


    @ApiOperation("柜体设计审核")
    @PostMapping("/design/confirmDesignReview")
    public ResultModel confirmDesignReview(
            @ApiParam(value = "批次柜体id", required = true) @RequestParam String batchProductId,
            //@ApiParam(value = "用户id",required = true) @RequestParam String userId,
            @ApiParam(value = "审核状态", required = true) @RequestParam int reviewState,
            @ApiParam(value = "审核备注") @RequestParam(required = false) String reviewMemo,
            @ApiParam(value = "附件") @RequestParam(value="file",required = false) MultipartFile multipartFile
    ) throws Exception {

        batchProductService.confirmDesignReview(batchProductId,reviewState,reviewMemo,multipartFile);

        BatchProduct batchProduct = batchProductRepository.findBatchProductById(batchProductId);

        if (reviewState == 2){

            // todo 若该柜体为批次内最后一个审核通过的柜体,更新批次柜体设计实际结束日期、物料采购实际开始日期
            List<BatchProduct> batchPlanProducts = batchProductRepository.findBatchProductByBatchPlanIdAndDelete(batchProduct.getBatchPlanId(), 0);
            boolean allProductPassReview = batchPlanProducts.stream().allMatch(product -> product.getReviewState() == 2);
            if (allProductPassReview) {
                PlanStage productDesign = planStageRepository.findPlanStageByPlanIdAndStageNameAndDelete(batchProduct.getBatchPlanId(), "柜体设计", 0);
                productDesign.setStageEndTime(new Date());
                planStageRepository.save(productDesign);

                PlanStage materialPurchase = planStageRepository.findPlanStageByPlanIdAndStageNameAndDelete(batchProduct.getBatchPlanId(), "物料采购", 0);
                materialPurchase.setStageStartTime(new Date());
                planStageRepository.save(materialPurchase);
            }

            batchProduct.setActualKickoffStartDate(new Date());

            // 变更图纸状态-审核通过,激活状态。对所有待审核的图纸文件进行状态激活
            List<ProductDrawingInfo> drawingInfos = productDrawingInfoRepository.findProductDrawingInfoByProductCodeAndChangeStatusAndDelete(batchProduct.getProductCode(), 0,0);
            for (ProductDrawingInfo item : drawingInfos){
                // 状态变白
                item.setChangeOrAdd("0");
                // 审核通过
                item.setChangeStatus(2);
                // 激活
                item.setIsActive(1);
            }
            productDrawingInfoRepository.saveAll(drawingInfos);
        }

        if (reviewState == 3){
            // 驳回后图纸状态默认被驳回
//            batchProduct.setIsElectDrawingUploaded(0);
//            batchProduct.setIsWireFileUploaded(0);

            // 驳回后图纸逻辑删除
            List<ProductDrawingInfo> drawingInfos = productDrawingInfoRepository.findProductDrawingInfoByProductCodeAndChangeStatusAndDelete(batchProduct.getProductCode(), 0,0);
            for (ProductDrawingInfo drawingInfo : drawingInfos){
//                drawingInfo.setDelete(1);
                drawingInfo.setChangeStatus(reviewState);
            }
            productDrawingInfoRepository.saveAll(drawingInfos);
        }

        batchProductRepository.save(batchProduct);

        return ResultModel.success("审核完成！");
    }

}
